/**
  ******************************************************************************
  * @file 	
  * @brief 
  * this encapsulates camera modules. Each module must adhere to the 
  * same interface. So if we do changesize it will call the driver for
  * the camera. Each camera interface must implement the methods required
  *
  *	Initially i'm thinking
  *	takesnapshot
  *	changeresolution
  *	changeformat 
  *	etc
  *
  * framewidth and height are in config.h
  ******************************************************************************
  */



#include "zbCAM.h"

// enable snapshot, this is all the camera does for now
// if we double buffer we should know which buffer we are on

/**
  * @brief  inits the cam and sends all config params for default values
  * @param  None
  * @retval None
  */
void initCAM()
{
	InitCamera();
}

void disableCAM()
{
	// not implemented
}

/**
  * @brief  Cause us to take a picture in the buffered image space. This is ment 
  * to be called when we start reading one image out the comm port we start to read
  * a new image into the "other" image space. This should be called at the beggining
  * of a read image command in the protocol
  * @param  None
  * @retval None
  */
void takeNextSnapShot()
{
   //printf("mygVars->VideoStat 0x%x\r\n",mygVars->VideoStat);
	// we were on B go back to 0
   if(mygVars->VideoStat >= 2)
   {
	   mygVars->VideoStat = 0;
   }
	mygVars->VideoStat++;
	DCMI_SnapShot();
}

/**
  * @brief  this basically only causes a picture to be taken for now
  * @param  None
  * @retval None
  */
void updateCAM()
{
	u8 cont = false;
	if(mygVars->enabled != newgVars->enabled)
	{
		// if we were off 
		if(!(mygVars->enabled & enSNAP))
		{
			// and now we are on
			if(newgVars->enabled & enSNAP)
			{
				//initCAM();
				// update to know that we are on now
				mygVars->enabled = (mygVars->enabled | enSNAP);	
				cont = true;
			}
		}
		// if we were on
		if(mygVars->enabled & enSNAP)
		{
			// and now we are off
			if(!(newgVars->enabled & enSNAP))
			{
				//disableCAM();
				// clear bit so we are off next time
				mygVars->enabled = (mygVars->enabled & !enSNAP);
			}
		}
	}
	
	// if we get to this point and we are disabled just ret
	if(!(mygVars->enabled & enSNAP) && (cont == false))
	{
		return;
	}

	// if its actually a different value do something 
	if(1)
	{

	}
		
}

void testCAM(void)
{
	int i=0, j=0;
	OV7670_IDTypeDef OV7670ID;

	if(DCMI_OV7670_ReadID(&OV7670ID)==0)
	{	
	if(OV7670ID.Manufacturer_ID1==0x7f || OV7670ID.Manufacturer_ID2==0xa2 ||OV7670ID.Version==0x73 ||OV7670ID.PID==0x76)
		printf("OV7670 ID:0x%x 0x%x 0x%x 0x%x\r\n",OV7670ID.Manufacturer_ID1, OV7670ID.Manufacturer_ID2, OV7670ID.PID, OV7670ID.Version);
	else
		printf("OV7670 ID is Error!\r\n");
	}
	
	if(DCMI_SingleRandomWrite(0x71, 0x80)==0) //color test	on
		printf("color test on..\r\n");
	else
		printf("color test on fails!!\r\n");

	while(debugChar == 'c')
	{
		printf("Taking snapshot\r\n");
		//DCMI_TakeSnapShot();
		Delay(2000);
		for(i=0; i<96; i++)
		{
			printf("\r\nLine: %d",i);
			for(j=0; j<128; j++)
			{
				if(j%32==0)
				{
					printf("\r\n");
				}
				//printf("%x",frame_buffer[i*128+j]);
				Delay(200);
			}
		}


	}

}
